<?php
/**
 * Description
 * User: duanwenjie
 * Date: 2019/5/9
 * Time: 11:30 AM
 */

namespace app\index\controller;

use app\common\ConfigBase;
use app\common\model\ToolModel;
use app\common\YujisoController;
use app\index\service\WarehouseService;
use think\Exception;

class Warehouse extends YujisoController
{
    public $param;
    public function __construct()
    {
        parent::__construct();
        $temp = $this->request->param();
        if (isset($temp['s'])) unset($temp['s']);
        $this->param = $temp;
    }

    /**
     * 获取仓库列表
     * @author duanwenjie
     */
    public function getWarehouseList()
    {
        $param = $this->param;
        $result = WarehouseService::getWarehouseList($param);
        returnJson('1','查询成功',$result['list'],$result['total']);
    }


    /**
     * 启用/禁用
     * @author duanwenjie
     */
    public function disableOrEnable()
    {
        $param =$this->param;
        $rules = [
            'id' => 'require',
        ];
        $this->validateData($rules);
        ToolModel::updateData(['id' => $param['id']], ['status' => $param['status']],'base_warehouse');
        returnJson(1,'操作成功');
    }


    /**
     * 新增/编辑
     * @author duanwenjie
     */
    public function edit()
    {
        try{
            $param = $this->param;
            $id = "";
            if ($this->request->isPost()){
                /*编辑*/
                if (isset($param['id']) && !empty($param['id'])){
                    $id = $param['id'];
                    unset($param['id']);
                }
                $res = WarehouseService::checkData($param, $id);
                $param['base_company_id'] = $res['base_company_id']; // 公司ID
                $param['base_shop_id'] = $res['base_shop_id']; // 店铺ID
                $param['base_country_id'] = $res['base_country_id']; // 国家ID
                $param['base_country_name'] = $res['base_country_name']; // 国家名称
                unset($param['company_name'], $param['shop_name']);
                if (!empty($id)){
                    ToolModel::updateData(['id' => $id], $param, 'base_warehouse');
                }else{
                    ToolModel::insertData($param, 'base_warehouse');
                }
                returnJson(1, "操作成功");
            }else{
                $result = WarehouseService::getWarehouseList($param);
                returnJson(1, '', $result['list']);
            }
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            returnJson(500, '失败:'.$e->getMessage());
        }
    }

    /**
     * 获取部分公司  2：零售公司 3：贸易及零售公司 (已启用)
     * 获取店铺（已启用）
     * @author duanwenjie
     */
    public function getShopCompanys()
    {
        $shops = ConfigBase::getShop();
        $companys = ConfigBase::getCompanys();
        exitJson(array("shops" => $shops,"companys" => $companys));
    }
}